import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms

# 创建张量 ,原教程中输入的变量是1,2,3，运行会报错
# RuntimeError: Only Tensors of floating point dtype can require gradients
# 所以把参数换成float类型
x=torch.tensor(1.0,requires_grad=True)
w=torch.tensor(2.0,requires_grad=True)
b=torch.tensor(3.0,requires_grad=True)

# 一条公式
y=w*x+b

# 计算梯度
y.backward()

# 打印结果 x:2.0 w:1.0 b:1.0
print('x:{} w:{} b:{}'.format(x.grad,w.grad,b.grad))